package com.shuyun.vertx.quickstart.test;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by feng.ding on 2017/2/8.
 */
public class LambdaPractice {

    public static void someMethod_1(){
        Collection<String> names = Lists.newArrayList();
        names.add("TaoBao");
        names.add("ZhiFuBao");
//        List<String> newNames = names.stream().map(String::toLowerCase).collect(Collectors.toList());
//        List<String> newNames = names.stream().map(name -> name.toLowerCase()).collect(Collectors.toList());
//        List<String> newNames = Lists.newArrayList();
//        for(String name : names){
//            newNames.add(name.toLowerCase());
//        }
        names.stream().map(name -> name.charAt(0)).collect(Collectors.toList());
        List<String> newNames = FluentIterable.from(names).transform(new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s.toLowerCase();
            }
        }).toList();
        System.out.println(newNames);
    }

    public static void someMethod_2(){
        String[] array = {"a", "b", "c"};
        for(Integer i : Lists.newArrayList(1, 2, 3)){
            Stream.of(array).map(item -> Strings.padEnd(item, i, '@')).forEach(System.out::println);
        }
    }

    public static void someMethod_3(){
        List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);
        System.out.println("sum is: " + nums.stream().filter(num -> num != null)
                                .distinct().mapToInt(num -> num * 2)
                                .peek(System.out::print).skip(2).limit(4).sum());
    }

    public static void someMethod_4(){
        List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);
        List<Integer> numsWithoutNull = nums.stream().filter(num -> num != null)
                        .collect(ArrayList::new, (list, item) -> list.add(item), (list1, list2) -> list1.addAll(list2));
        List<Integer> numsWithoutNull_2 = nums.stream().filter(num -> num != null)
                .collect(Collectors.toList());
    }

    public static void someMethod_5(){
        List<Integer> ints = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);
        System.out.println(ints.stream().allMatch(item -> item < 100));
        ints.stream().max((o1, o2) -> o1.compareTo(o2)).ifPresent(System.out::println);
        System.out.println("ints sum is: " + ints.stream().reduce((sum, item) -> sum + item).get());
    }

    public static void main(String[] args) {
        someMethod_5();
    }
}